package com.jewaton.auth.controller;

import com.jewaton.auth.entity.Resources;
import com.jewaton.auth.entity.User;
import com.jewaton.auth.service.ResourcesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private ResourcesService resourcesService;
	
    @RequestMapping(value = {"/","/index.html"})
    public String index(Model model)
    {
        return "/framework/main";
    }

    @RequestMapping ("top")
    public String top(Model model)
    {
        return "/framework/top";
    }

    @RequestMapping ("left")
    public String left(Model model,HttpServletRequest request)
    {
        try {
            String username = request.getUserPrincipal().getName();
            List<Resources> resources = resourcesService.getResourcesByUserName(username);
            model.addAttribute("resources", resources);
        } catch (Exception e) {
            logger.error(e.toString(),e);
        }
        return "/framework/left";
    }

    @RequestMapping ("getMenus")
    @ResponseBody
    public List<Resources> getMenus(Model model,HttpServletRequest request)
    {
        List<Resources> resources = new ArrayList<Resources>();
        try {
            String username = request.getUserPrincipal().getName();
            resources = resourcesService.getMenusByUserName(username);
            //model.addAttribute("resources", resources);
        } catch (Exception e) {
            logger.error(e.toString(),e);
        }
        return resources;
    }

    @RequestMapping ("tab")
    public String tab(Model model)
    {
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        model.addAttribute("admin", user);
        return "/framework/tab/tab";
    }

    @RequestMapping ("center")
    public String center(Model model)
    {
        return "/framework/center";
    }
	
	@RequestMapping(value = "/login.html")
	public String login() {
		
		return "login";
	}
	
	@RequestMapping(value = "/loginerr.html")
	public String loginError(Model model) {
		model.addAttribute("loginError", true);
		return "login";
	}
	
	@RequestMapping(value = "/403.html")
	public String accessDenied() {
		return "403";
	}
	
}
